package toy.keli.picmap.util;

public class utilGps {
    static double pi = 3.14159265358979324;

    static double a = 6378245.0;

    static double ee = 0.00669342162296594323;

    public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    public static double[] wgs2bd(double lat, double lon) {
        double[] wgs2gcj = wgs2gcj(lat, lon);

        double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]);

        return gcj2bd;

    }

    public static double[] gcj2bd(double lat, double lon) {
        double x = lon, y = lat;

        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);

        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);

        double bd_lon = z * Math.cos(theta) + 0.0065;

        double bd_lat = z * Math.sin(theta) + 0.006;

        return new double[] { bd_lat, bd_lon };

    }
    //Java工具 - 坐标系WGS-84，GCJ-02，BD-09之间的相互转换  https://www.cnblogs.com/helios-fz/p/11177080.html
    public static class bd2wgs{
        static double PI = 3.14159265358979324;
        static double A = 6378245.0;
        static double EE = 0.00669342162296594323;
        public static double[] call(double lat, double lon){
            double[] a = bd2gcj(lat, lon);
            double[] b = gcj2gps(a[0], a[1]);
            return b;
        }
        /**
         * 火星坐标系 GCJ-02 to 地球坐标系 WGS-84
         *
         * @param latitude  纬度
         * @param longitude 经度
         * @return [纬度, 经度]
         */
        public static double[] gcj2gps(double latitude, double longitude) {
            Point dev = calDev(latitude, longitude);
            double retLat = latitude - dev.getLatitude();
            double retLon = longitude - dev.getLongitude();
            dev = calDev(retLat, retLon);
            retLat = latitude - dev.getLatitude();
            retLon = longitude - dev.getLongitude();
            return new double[]{retLat, retLon};
        }

        private static Point calDev(double latitude, double longitude) {
    //        if (isOutOfChina(latitude, longitude, false)) {
    //            return new Point(latitude, latitude);
    //        }
            double dLat = calLat(longitude - 105.0, latitude - 35.0);
            double dLon = calLon(longitude - 105.0, latitude - 35.0);
            double radLat = latitude / 180.0 * PI;
            double magic = Math.sin(radLat);
            magic = 1 - EE * magic * magic;
            double sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
            dLon = (dLon * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);
            return new Point(dLat, dLon);
        }

        private static double calLat(double x, double y) {
            double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
            ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        private static double calLon(double x, double y) {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
            ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
            return ret;
        }

        static class Point {
            private double longitude;
            private double latitude;

            Point(double latitude, double longitude) {
                this.longitude = longitude;
                this.latitude = latitude;
            }

            public double getLongitude() {
                return longitude;
            }

            public void setLongitude(double longitude) {
                this.longitude = longitude;
            }

            public double getLatitude() {
                return latitude;
            }

            public void setLatitude(double latitude) {
                this.latitude = latitude;
            }

            @Override
            public String toString() {
                return longitude + "," + latitude;
            }
        }

    }
    public static double[] bd2gcj(double lat, double lon) {
        double x = lon - 0.0065, y = lat - 0.006;

        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);

        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

        double gg_lon = z * Math.cos(theta);

        double gg_lat = z * Math.sin(theta);

        return new double[] { gg_lat, gg_lon };

    }

    public static double[] wgs2gcj(double lat, double lon) {
        double dLat = transformLat(lon - 105.0, lat - 35.0);

        double dLon = transformLon(lon - 105.0, lat - 35.0);

        double radLat = lat / 180.0 * pi;

        double magic = Math.sin(radLat);

        magic = 1 - ee * magic * magic;

        double sqrtMagic = Math.sqrt(magic);

        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);

        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);

        double mgLat = lat + dLat;

        double mgLon = lon + dLon;

        double[] loc = { mgLat, mgLon };

        return loc;

    }

    private static double transformLat(double lat, double lon) {
        double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));

        ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;

        ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;

        ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0;

        return ret;

    }

    private static double transformLon(double lat, double lon) {
        double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));

        ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;

        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;

        ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;

        return ret;

    }

}